package com.tuanbiao.ui.plan.adapter;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hzjq.data.entity.ACapEntity;
import com.tuanbiao.R;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/13 14:19
 */
public class DetonatorSingleCheckAdapter extends BaseQuickAdapter<ACapEntity, BaseViewHolder> {

    public DetonatorSingleCheckAdapter() {
        super(R.layout.item_detonator_single);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, ACapEntity detonatorSingle) {
        Timber.i("" + detonatorSingle);
        holder.setText(R.id.tv_num, getContext().getString(R.string.single_check_num_desc, holder.getLayoutPosition() + 1))
                .setText(R.id.tv_detonator_code, getContext().getString(R.string.single_check_detonator_code_desc, detonatorSingle.getConvertUid()))
                .setText(R.id.tv_info, getContext().getString(R.string.single_check_info_desc, detonatorSingle.getMVoltage(), detonatorSingle.getMElectric()))
                .setText(R.id.tv_status, getStatus(detonatorSingle));
        if (detonatorSingle.getMElectric() <= 30 && detonatorSingle.getError() == 0) {
            holder.setTextColorRes(R.id.tv_status, R.color.textSuccess);
        } else {
            holder.setTextColorRes(R.id.tv_status, R.color.textDanger);
        }
    }

    private String getStatus(ACapEntity aCapEntity) {
        if (aCapEntity.getMElectric() <= 30 && aCapEntity.getError() == 0) {
            return "正常";
        } else {
            if (aCapEntity.getMElectric() > 30) {
                return "电流异常";
            } else {
                return aCapEntity.getErrorMsg();
            }
        }
    }

    /**
     * 错误信息(简洁文案)
     */
    private String checkErrorCode2(int stateCode) {
        String str = "";
        if (-8 == stateCode) {
            str = "授权失败";
        } else if (-12 == stateCode) {
            str = "写延时失败";
        } else if (-16 == stateCode) {
            str = "校时失败";
        } else if (-20 == stateCode) {
            str = "充电失败";
        } else if (62 == stateCode) {
            str = "使能失败";
        } else if (stateCode == 0) {
            return "正常";
        } else if (-74 == stateCode) {
            return "雷管未授权";
        }
        return str;
    }
}
